package com.example.model;

import java.util.List;
import java.util.ArrayList;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;

import java.io.UnsupportedEncodingException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.controller.*;
import com.example.model.DatabaseAccess;
import com.example.model.DatabaseAccess.ExecutionType;

public class ServiceCRUD implements iCRUD2<Service, Integer, Integer> {

	@Override
	public void create(Service instance) {
		HttpPost httpPost = new HttpPost("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectServiceTask/helpConnectService");
		
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("userID", Integer.toString(instance.getUserID())));
		nameValuePairs.add(new BasicNameValuePair("description", instance.getDescription()));
		nameValuePairs.add(new BasicNameValuePair("cost", Double.toString(instance.getCost())));
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		} catch(UnsupportedEncodingException e) { 
			e.printStackTrace();
		}
		
		DataAccessBundle dab = new DataAccessBundle(httpPost, ExecutionType.CREATE);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		dba.doInBackground(dabArr);
		
	}

	@Override
	public Service read(Integer ID) {
		
		HttpGet httpGet = new HttpGet("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectServiceTask/service*&*" + Integer.toString(ID));
		
		String result = null;
		
		DataAccessBundle dab = new DataAccessBundle(httpGet, ExecutionType.READ);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		result = dba.doInBackground(dabArr);
		
		Service service = new Service(new ServiceCRUD());
		if(result != null)
		{
			try {
				JSONObject record = new JSONObject(result);
				//System.out.println(record.toString());
				//System.out.println(Integer.toString(jSonArray.length()));
				if(record != null){
					service.setServiceID(record.getInt("ServiceID"));
					service.setUserID(record.getInt("UserID"));
					service.setDescription((record.getString("Description") == "null" || record.getString("Description") == "") ? "": record.getString("Description"));
					service.setCost(record.getDouble("Cost"));
				}
			} catch (JSONException e) {
				
				e.printStackTrace();
			}
		}
		
		return service;
		
		
		
	}

	@Override
	public List<Service> readAll() {

		HttpGet httpGet = new HttpGet("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectServiceTask/all*&*none");
		
		String result = null;
		
		DataAccessBundle dab = new DataAccessBundle(httpGet, ExecutionType.READ);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		result = dba.doInBackground(dabArr);
				
		List<Service> serviceList = new ArrayList<Service>();
		
		if(result != null)
		{
			//System.out.println(result);
			try {
				JSONArray jSonArray = new JSONArray(result);
				//System.out.println(jSonArray.toString());
				//System.out.println(Integer.toString(jSonArray.length()));
				if(jSonArray.length() > 0){
					for(int i = 0; i < jSonArray.length(); i++){
						Service service = new Service(new ServiceCRUD());
						JSONObject record = jSonArray.getJSONObject(i);
						
						service.setServiceID(record.getInt("ServiceID"));
						service.setUserID(record.getInt("UserID"));
						service.setDescription((record.getString("Description") == "null" || record.getString("Description") == "") ? "": record.getString("Description"));
						service.setCost(record.getDouble("Cost"));
						
						serviceList.add(service);
						
					}
				}
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		return serviceList;
		
	}
	
	@Override
	public List<Service> readAllByUser(int userID) {
		
		HttpGet httpGet = new HttpGet("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectServiceTask/user*&*" + Integer.toString(userID));
				
		String result = null;
		
		DataAccessBundle dab = new DataAccessBundle(httpGet, ExecutionType.READ);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		result = dba.doInBackground(dabArr);
		
		System.out.println(result);
		
		List<Service> serviceList = new ArrayList<Service>();
		
		if(result != null)
		{
			//System.out.println(result);
			try {
				JSONArray jSonArray = new JSONArray(result);
				//System.out.println(jSonArray.toString());
				//System.out.println(Integer.toString(jSonArray.length()));
				if(jSonArray.length() > 0){
					for(int i = 0; i < jSonArray.length(); i++){
						Service service = new Service(new ServiceCRUD());
						JSONObject record = jSonArray.getJSONObject(i);
						
						service.setServiceID(record.getInt("ServiceID"));
						service.setUserID(record.getInt("UserID"));
						service.setDescription((record.getString("Description") == "null" || record.getString("Description") == "") ? "": record.getString("Description"));
						service.setCost(record.getDouble("Cost"));
						
						serviceList.add(service);
						
					}
				}
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		return serviceList;
		
	}
	
	@Override
	public List<Service> readAllByKeyword(String keyword) {

		HttpGet httpGet = new HttpGet("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectServiceTask/description*&*" + keyword);
		
		String result = null;
		
		DataAccessBundle dab = new DataAccessBundle(httpGet, ExecutionType.READ);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		result = dba.doInBackground(dabArr);
		
		System.out.println(result);
		
		List<Service> serviceList = new ArrayList<Service>();
		
		if(result != null)
		{
			//System.out.println(result);
			try {
				JSONArray jSonArray = new JSONArray(result);
				//System.out.println(jSonArray.toString());
				//System.out.println(Integer.toString(jSonArray.length()));
				if(jSonArray.length() > 0){
					for(int i = 0; i < jSonArray.length(); i++){
						Service service = new Service(new ServiceCRUD());
						JSONObject record = jSonArray.getJSONObject(i);
						
						service.setServiceID(record.getInt("ServiceID"));
						service.setUserID(record.getInt("UserID"));
						service.setDescription((record.getString("Description") == "null" || record.getString("Description") == "") ? "": record.getString("Description"));
						service.setCost(record.getDouble("Cost"));
						
						serviceList.add(service);
						
					}
				}
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		return serviceList;
		
	}
	

	@Override
	public void update(Service instance) {
		
		HttpPost httpPut = new HttpPost("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectServiceTask/" + Integer.toString(instance.getServiceID()));
		httpPut.addHeader("X-HTTP-Method-Override", "PUT");
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("description", instance.getDescription()));
		nameValuePairs.add(new BasicNameValuePair("cost", Double.toString(instance.getCost())));
		try {
			httpPut.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		} catch (UnsupportedEncodingException e) { 
			e.printStackTrace();
		}
		
		DataAccessBundle dab = new DataAccessBundle(httpPut, ExecutionType.UPDATE);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		dba.doInBackground(dabArr);
		
	}

	@Override
	public void delete(Integer ID) {
		
		HttpDelete httpDelete = new HttpDelete("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectServiceTask/" + Integer.toString(ID));

		DataAccessBundle dab = new DataAccessBundle(httpDelete, ExecutionType.DELETE);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		dba.doInBackground(dabArr);
		
	}

	
}
